﻿@page "/importexportinfo/update/{Id}"
@using Compass.Wasm.Shared.Logistics
@using Compass.Wasm.Client.Services.Logistics
@inject IImportExportInfoService ImportExportInfoService;
@inject NavigationManager NavigationManager
@inject IJSRuntime JsRuntime


<PageTitle>更新进出口货物信息</PageTitle>
<div class="row">
    <div class="col">
        <p class="fs-5" style="font-weight:bold;">更新进出口货物信息</p>
    </div>
    <CompDeleteIcon Roles="admin,log" Click="Delete" />
</div>
<EditForm Model="_dto" OnSubmit="Save">
    <div class="input-group mb-2 mt-2">
        <span class="input-group-text">料号</span>
        <input class="form-control" placeholder="料号(必填)" @bind-value="@_dto.MtlNumber" />
    </div>
    <div class="input-group mb-2">
        <span class="input-group-text">类型</span>
        <select class="form-select" @bind="@_dto.Type">
            <CompEnumList Type="typeof(ImportExportType_e)" />
        </select>
    </div>
    <div class="input-group mb-2">
        <span class="input-group-text">BU</span>
        <select class="form-select" @bind="@_dto.Sbu">
            <CompEnumList Type="typeof(Sbu_e)" />
        </select>
    </div>
    <div class="input-group mb-2 mt-2">
        <span class="input-group-text">品名</span>
        <input class="form-control" placeholder="品名(必填)" @bind-value="@_dto.Name" />
    </div>
    <div class="input-group mb-2 mt-2">
        <span class="input-group-text">HS编码</span>
        <input class="form-control" placeholder="HS编码(必填)" @bind-value="@_dto.HsCode" />
    </div>
    <a href="http://old.hscode.net/IntegrateQueries/QueryYS" target="_blank">申报要素查询链接</a>
    <div class="input-group mb-2 mt-2">
        <span class="input-group-text">HS描述</span>
        <input class="form-control" placeholder="HS描述(必填)" @bind-value="@_dto.HsDescription" />
    </div>
    <div class="input-group mb-2 mt-2">
        <span class="input-group-text">产地</span>
        <input class="form-control" placeholder="产地(必填)" @bind-value="@_dto.PlaceOfOrigin" />
    </div>
    <div class="input-group mb-2">
        <span class="input-group-text">申报要素</span>
        <textarea class="form-control" placeholder="申报要素(必填)，可输入多行" style="height: 350px" @bind="@_dto.DeclarationElements"></textarea>
    </div>
    <div class="input-group mb-2 mt-2">
        <span class="input-group-text">净重</span>
        <input class="form-control" placeholder="净重" @bind-value="@_dto.NetWeight" />
    </div>
    <div class="input-group mb-2 mt-2">
        <span class="input-group-text">PO</span>
        <input class="form-control" placeholder="以前采购过的订单号码" @bind-value="@_dto.PoNumbers" />
    </div>

    <div class="input-group mb-2">
        <span class="input-group-text">内部描述</span>
        <textarea class="form-control" placeholder="内部描述，可输入多行" style="height: 350px" @bind="@_dto.NavDescription"></textarea>
    </div>

    <MultiFileUpload Title="附件(可添加多个)" FileType="application/pdf" @bind-MultiFileUrl="@_dto.AttachmentsUrl" />

    <ImagesUpload Title="图片(可添加多个)" @bind-MultiImageUrl="@_dto.ImagesUrl" />
    

    <CompSaveCancelButtons CancelUrl="importexportinfos" />
</EditForm>

@code {
    [Parameter]
    public string Id { get; set; }
    private ImportExportInfoDto _dto = new();
    protected override async Task OnInitializedAsync()
    {
        var response = await ImportExportInfoService.GetFirstOrDefaultAsync(Guid.Parse(Id));
        if (!response.Status) NavigationManager.NavigateTo("importexportinfos");
        _dto = response.Result;        
    }
    private async Task Delete()
    {
        var confirmResult = await JsRuntime.InvokeAsync<bool>("confirm", $"确定要删除品名【{_dto.Name}】吗？");
        if (confirmResult)
        {
            var result = await ImportExportInfoService.DeleteAsync(Guid.Parse(Id));
            if (result.IsSuccessStatusCode) NavigationManager.NavigateTo("importexportinfos");
        }
    }
    private async Task Save()
    {
        if (string.IsNullOrEmpty(_dto.MtlNumber) || string.IsNullOrEmpty(_dto.Name)|| string.IsNullOrEmpty(_dto.HsCode)|| string.IsNullOrEmpty(_dto.HsDescription)|| string.IsNullOrEmpty(_dto.PlaceOfOrigin)|| string.IsNullOrEmpty(_dto.DeclarationElements))
        {
            await JsRuntime.InvokeVoidAsync("alert", "请填写所有的必填项");
            return;
        }
        //为了避免号前后有空格，此处消除空格
        _dto.MtlNumber = _dto.MtlNumber.Trim();
        _dto.HsCode = _dto.HsCode.Trim();
        var result = await ImportExportInfoService.UpdateAsync(Guid.Parse(Id), _dto);
        if (result.IsSuccessStatusCode) NavigationManager.NavigateTo("importexportinfos");
    }
}
